#pragma once
#include <iostream>
#include <jsoncpp/json/json.h>
const std::string Sep = "\r\n";//分隔符
bool Encode(std::string& message)
{
    if(message.size()==0)
    {
        return false;
    }
    std::string package;
    package = std::to_string(message.size())+Sep+message+Sep;
    message = package;
    return true;
}
bool Decode(std::string& package,std::string* content)
{
    auto pos = package.find(Sep);
    if(pos==std::string::npos)
    {
        return false;
    }
    //legth\r\n{ json }\r\n
    int length = std::stoi(package.substr(0,pos));
    int full_length = length+2*Sep.size()+pos;
    //legth\r\n{ json 类似于这样的不完整的报文
    if(package.size()<full_length)
    {
        return false;
    }
    //完整的报文，解码
    *content = package.substr(pos+Sep.size(),length);
    package.erase(0,full_length);
    return true;
}
class Request
{
public:
    Request()
    :_x(0),_y(0),_oper(0)
    {}
    Request(int x,char oper,int y)
    :_x(x),_y(y),_oper(oper)
    {

    }
    bool Serialize(std::string& outstring) 
    {
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;
        //将jason转换为字符串
        Json::StreamWriterBuilder writer;
        outstring = writeString(writer,root);
        return true;
    }
    bool Deserialize(const std::string& jsonString)
    {
        Json::Value root;
        Json::CharReaderBuilder reader;
        std::string errs;
        std::istringstream s(jsonString);

        bool praseSuccess = Json::parseFromStream(reader,s,&root,&errs);
        if(!praseSuccess)
        {
            std::cerr<<"Failed to parse Json: "<<errs<<std::endl;
            return false;
        }
        //检查必要字段是否存在
        if(!root.isMember("x")||!root.isMember("oper")||!root.isMember("y"))
        {
            std::cerr<<"Missing fields in Json"<<std::endl;
            return false;
        }
        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();
        //_oper = root["oper"].asString()[0];//oper是一个字符
        return true;
    }
    void Print()
    {
        std::cout<<_x<<" "<<_oper<<" "<<_y<<std::endl;
    }
    int X()
    {
        return _x;
    }
    int Y()
    {
        return _y;
    }
    const char Oper()
    {
        return _oper;
    }
private:
    int _x;
    int _y;
    char _oper;
};
class Response
{
public:
    Response()
    :_result(0),_code(0)
    {

    }
    bool Serialize(std::string& outstring) 
    {
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;
        //将jason转换为字符串
        Json::StreamWriterBuilder writer;
        outstring = writeString(writer,root);
        return true;
    }
    bool Deserialize(const std::string& jsonString)
    {
        Json::Value root;
        Json::CharReaderBuilder reader;
        std::string errs;
        std::istringstream s(jsonString);

        bool praseSuccess = Json::parseFromStream(reader,s,&root,&errs);
        if(!praseSuccess)
        {
            std::cerr<<"Failed to parse Json: "<<errs<<std::endl;
            return false;
        }
        //检查必要字段是否存在
        if(!root.isMember("result")||!root.isMember("code"))
        {
            std::cerr<<"Missing fields in Json"<<std::endl;
            return false;
        }
        _result = root["result"].asInt();
        _code = root["code"].asInt();
        return true;
    }
    void SetResult(int result)
    {
        _result = result;
    }
    void SetCode(int code)
    {
        _code = code;
    }
    void Print()
    {
        std::cout<<_result<<"["<<_code<<"]"<<std::endl;
    }
private:
    int _result;//计算结果
    int _code;//错误码 0：成功  非0:失败
};